# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _


class SaleOrder(models.Model):
    _inherit = "fp.sale.order"

    partner_state = fields.Selection(
        related='partner_id.nc_state', string='partner NC Status', store=True)

    product_state = fields.Selection([
        ('draft', 'Draft'),
        ('done', 'Done'),
        ('error', 'Error'),
    ], string='product Nc Status', compute='_compute_product_state', store=True)

    @api.depends('line_ids', 'line_ids.product_state')
    def _compute_product_state(self):
        product_state = 'done'
        for one in self:
            for line in one.line_ids:
                if line.product_state != 'done':
                    product_state = 'draft'
                break
            one.product_state = product_state


class BizPartner(models.Model):
    _inherit = "fp.biz.partner"

    nc_state = fields.Selection([
        ('draft', 'Draft'),
        ('done', 'Done'),
        ('error', 'Error'),
    ], string='Nc Status',  compute='_compute_nc_state', store=True)

    nc_partner_ids = fields.One2many(
        'fp.nc.ods.customer', 'partner_id', string='nc.ods.customer', readonly=True)

    @api.depends('nc_partner_ids.state')
    def _compute_nc_state(self):
        for rec in self:
            if rec.nc_partner_ids:
                rec.nc_state = rec.nc_partner_ids[0].state
            else:
                rec.nc_state = 'draft'

    def action_done(self):
        self2 = self.search(
            [('id', 'in', self.ids), ('nc_state', '=', 'draft')])

        for one in self2:
            nc_partner_ids = one.mapped('nc_partner_ids')
            if nc_partner_ids:
                nc_partner_ids.action_done()
            else:
                one.nc_state = 'done'

    def action_draft(self):
        self2 = self.search([('id', 'in', self.ids),
                             ('nc_state', 'in', ['done', 'error'])])

        for one in self2:
            nc_partner_ids = one.mapped('nc_partner_ids')
            if nc_partner_ids:
                nc_partner_ids.action_draft()
            else:
                one.nc_state = 'draft'


class NcOdsCustomer(models.Model):
    _name = "fp.nc.ods.customer"
    _description = "NC ODS Customer"

    state = fields.Selection([
        ('draft', 'Draft'),
        ('done', 'Done'),
        ('error', 'Error'),
    ], string='Status', readonly=True, copy=False, index=True, default='draft')

    partner_id = fields.Many2one(
        'fp.biz.partner', string='Partner', index=True)

    company_code = fields.Char('公司代码')
    company_name = fields.Char('公司名称')

    name = fields.Char(string='名称')
    code = fields.Char(string='编码')
    name2 = fields.Char(string='开票名称')
    code2 = fields.Char(string='开票编码')
    tin = fields.Char(string='税号')
    phone = fields.Char(string='电话')
    address = fields.Char(string='地址')
    address_phone = fields.Char(string='地址电话')
    bank_name = fields.Char(string='开户行')
    acc_number = fields.Char(string='开户账户')
    bank_account = fields.Char(string='开户行及账户')

    def action_done(self):
        self2 = self.search([('id', 'in', self.ids), ('state', '=', 'draft')])
        for one in self2:
            one.write({'state': 'done'})

    def action_draft(self):
        self2 = self.search(
            [('id', 'in', self.ids), ('state', 'in', ['done', 'error'])])
        for one in self2:
            one.write({'state': 'draft'})

    @api.model
    @api.returns('self', lambda value: value.ids)
    def import_create_for_nc(self, vals_list):
        records = []
        for one in vals_list:
            code = one['code']
            old = self.search([('code', '=', code)])
            if not old:
                records.append(one)

        new_result = self.create(records)

        return new_result

    @api.returns('self', lambda value: value.ids)
    def load_to_model(self):
        return self._load_to_ods()

    def _load_to_ods(self):
        ptn = self.env['fp.partner']
        biz = self.env['fp.biz.partner']

        for one in self:
            if one.code2:
                ptn2 = ptn.search([('code', '=', one.code2)], limit=1)
                if not ptn2:
                    ptn2 = ptn.create({
                        'name': one.name2,
                        'fapiao_name': one.name2,
                        'shortname': one.name2,
                        'code': one.code2,
                        'tin': one.tin,
                        'phone': one.phone,
                        'address': one.address,
                        'bank_name': one.bank_name,
                        'acc_number': one.acc_number,
                        'bank_account': one.bank_account,
                    })
            else:
                ptn2 = self.env['fp.partner']

            biz2 = biz.search([('code', '=', one.code)], limit=1)
            if not biz2:
                biz2 = biz.create({
                    'name': one.name,
                    'code': one.code,
                    'fp_partner_id': ptn2.id,
                })

            one.partner_id = biz2

            biz = biz | biz2

        return biz


"""


odoo. 
so, pid = bizid (ncid),  bizid.fp_partner_id(maybe null ), fp.partner

wizard=  so.partner.wizard
{
   biz_id = m2o( fp.biz.partner,  =active_id )
   fp_partner_id = m2o(related='biz_id.fp_partner_id')
   nc_id =  m2o (nc.ods.customer, biz_id.ods_ids )
   
   code,name : readonly
   code2,name2:  readonly= new and 0 or '1'
   ...tin.....

}

{
   fp.biz.partner (id, code, name, fp_partner_id)
   nc.ods.customer(  code, name, code2(maybe null), name2(maybe null) )
   
   1. if fp_partner_id.  cehck ok. 
       pass 
   2. else if fp_partner_id. change fp_partner_id
        update nc.ods.customer
        update fp.biz.partner
        
   3. else fp_partner_id=null
        set nc.ods.customer,  name2. code2
        fp.biz.partner: fp_partner_id=new (name2. code2)

   all: p.biz.partner. state=done
 
}


"""
